<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Code by TinhNQ
 * 2014
 **/
class Restaurant_model extends MY_Model {

    protected $restaurant;

    function __construct(){
        parent::__construct();
    }

    function table_name(){
        return "restaurants";
    }

    function get_restaurants($filters = array(),$counting = false){
        if (isset($this->CI->cache)){
            $caching = true;
            $cache_key = 'ci--Restaurant_'.md5(serialize($filters));
            if ($counting){
                $cache_key = 'ci--Restaurant_COUNT_'.md5(serialize($filters));
            }
            if ($return = $this->cache->file->get($cache_key)) {
                return ($return == 'empty_cache') ? FALSE : $return;
            }
        }else{
            $caching = false;
        }

        $this->db->select('*');
        $this->db->from('restaurants');
        $param = array();

        if (isset($filters['restaurant_id'])) {
            $this->db->where('restaurant_id', $filters['restaurant_id']);
        }
        if (isset($filters['restaurant_id_other'])) {
            $this->db->where('restaurant_id !=', $filters['restaurant_id_other']);
        }
        $this->db->order_by('restaurant_id','DESC');
        if (isset($filters['name'])) {
            $this->db->where('name', $filters['name']);
        }
        if (isset($filters['alias'])) {
            $this->db->where('alias', $filters['alias']);
        }
        if (isset($filters['introtext'])) {
            $this->db->where('introtext', $filters['introtext']);
        }
        if (isset($filters['fulltext'])) {
            $this->db->where('fulltext', $filters['fulltext']);
        }
        if (isset($filters['image1'])) {
            $this->db->where('image1', $filters['image1']);
        }
        if (isset($filters['image2'])) {
            $this->db->where('image2', $filters['image2']);
        }
        if (isset($filters['image3'])) {
            $this->db->where('image3', $filters['image3']);
        }
        if (isset($filters['image4'])) {
            $this->db->where('image4', $filters['image4']);
        }
        if (isset($filters['image5'])) {
            $this->db->where('image5', $filters['image5']);
        }
        if (isset($filters['is_hot'])) {
            $this->db->where('is_hot', $filters['is_hot']);
        }
        if (isset($filters['featured'])) {
            $this->db->where('featured', $filters['featured']);
        }
        if (isset($filters['region_id'])) {
            $this->db->where('region_id', $filters['region_id']);
        }
        if (isset($filters['pref_id'])) {
            $this->db->where('pref_id', $filters['pref_id']);
        }
        if (isset($filters['city_id'])) {
            $this->db->where('city_id', $filters['city_id']);
        }
        if (isset($filters['address'])) {
            $this->db->where('address', $filters['address']);
        }
        if (isset($filters['localtion'])) {
            $this->db->where('localtion', $filters['localtion']);
        }
        if (isset($filters['tel'])) {
            $this->db->where('tel', $filters['tel']);
        }
        if (isset($filters['url'])) {
            $this->db->where('url', $filters['url']);
        }
        if (isset($filters['status'])) {
            $this->db->where('status', $filters['status']);
        }
        if (isset($filters['ordering'])) {
            $this->db->where('ordering', $filters['ordering']);
        }
        if (isset($filters['create_by'])) {
            $this->db->where('create_by', $filters['create_by']);
        }
        if (isset($filters['created_to'])) {
            $this->db->where('created <=', $filters['created_to']);
        }
        if (isset($filters['created_from'])) {
            $this->db->where('created =>', $filters['created_from']);
        }
        if (isset($filters['update_by'])) {
            $this->db->where('update_by', $filters['update_by']);
        }
        if (isset($filters['update_to'])) {
            $this->db->where('update <=', $filters['update_to']);
        }
        if (isset($filters['update_from'])) {
            $this->db->where('update =>', $filters['update_from']);
        }


        if (isset($filters['limit'])) {
            $offset = (isset($filters['page'])) ? $filters['page'] : 0;
            $this->db->limit($filters['limit'], $offset);
        }

        $result = $this->db->get();

        if($counting !=false){
            $rows = $result->num_rows();
            $result->free_result();

            if ($caching == TRUE) {
                $this->CI->cache->file->save($cache_key, $rows, (5*60));
            }

            return $rows;
        }
        if ($result->num_rows() == 0) {
            return false;
        }
        $restaurant = array();
        foreach ($result->result_array() as $row ){
            $row['link'] = create_url('restaurant/'.$row['alias'],array('rewrite'=>true));
            $row['date'] = fdate($row['created'],"%A, %d/%m/%Y - %H:%M");
            $restaurant[] = $row;
        }
        if ($caching){
            $this->CI->cache->file->save($cache_key,$restaurant);
        }
        $result->free_result();

        return $restaurant;
    }

    function get_count($filter = array()){
        return $this->get_restaurants($filter,true);
    }

    function get_restaurant($restaurantId){
        if (!isset($this->restaurant[$restaurantId])){
            $restaurant = $this->get_restaurants(array('restaurant_id'=>$restaurantId));
            $this->restaurant[$restaurantId] = $restaurant;
        }else{
            $restaurant = $this->restaurant[$restaurantId];
        }
        if (empty($restaurant)) {
            return false;
        }
        return $restaurant[0];
    }

    function insert_restaurant($data){
        $_restaurant_id = $this->save($data);
        return $_restaurant_id;
    }

    function update_restaurant($data, $restaurant_id){
        return $this->update($attributes, array('restaurant_id' => $restaurant_id));
    }

    function delete_restaurant($restaurant_id) {
        return $this->delete(array('restaurant_id'=>$restaurant_id));
    }

    function change_status_restaurant($restaurant_id) {
        $status = $this->db->select('status')->from('restaurant')->where('restaurant_id', $restaurant_id)->get()->result_array();

        if ($status[0]['status'] == 1) {
            $change = 0;
        } else {
            $change = 1;
        }
        return $this->update(array('status' => $change), array('restaurant_id' => $restaurant_id));
    }

    function get_restaurant_by_alias($restaurant_alias){
        $restaurant = $this->get_restaurants(array('alias'=>$restaurant_alias),false);
        if ($restaurant){
            return $restaurant[0];
        }
        return false;
    }

}
?>